package at.fhooe.mc.duel_game.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import at.fhooe.mc.duel_game.*;

public class ResultActivity extends Activity implements OnClickListener {

	boolean isTraining = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_result);

		Button b1 = (Button) findViewById(R.id.ButtonRestart);
		b1.setOnClickListener(this);
		Button b2 = (Button) findViewById(R.id.ButtonBackToMenu);
		b2.setOnClickListener(this);

		Intent i = getIntent();
		DuelResult duelResult = (DuelResult) i.getParcelableExtra("RESULT");
		String text;

		if (duelResult.isWin()) {
			text = getResources().getString(R.string.resultWin) + "\n\n"
					+ getResources().getString(R.string.resultName) + " "
					+ duelResult.getPlayer() + "\n\n"
					+ getResources().getString(R.string.resultMyTime) + " "
					+ duelResult.getMyTime() + " ms\n";
			if (duelResult.getOpponentTime() != Long.MAX_VALUE) {
				text += getResources().getString(R.string.resultOpponentTime)
						+ " " + duelResult.getOpponentTime() + " ms";
				isTraining = false;
			} else {
				isTraining = true;

			}
			
			SoundManager.playSound(SoundManager.SOUND_CLAP);
		} else {
			text = getResources().getString(R.string.resultLose) + "\n\n"
					+ getResources().getString(R.string.resultName) + " "
					+ duelResult.getPlayer() + "\n\n"
					+ getResources().getString(R.string.resultMyTime) + " "
					+ duelResult.getMyTime() + " ms\n"
					+ getResources().getString(R.string.resultOpponentTime)
					+ " " + duelResult.getOpponentTime() + " ms";
			
			SoundManager.playSound(SoundManager.SOUND_DEATH);
		}

		TextView textView = (TextView) findViewById(R.id.textViewResult);
		textView.setText(text);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ButtonRestart:
			Intent intent1 = new Intent(this.getApplicationContext(),
					GameActivity.class);

			intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			if (isTraining) {
				intent1.putExtra("TRAINING", isTraining);
			}
			startActivity(intent1);
			break;
		case R.id.ButtonBackToMenu:
			Intent intent2 = new Intent(this.getApplicationContext(),
					GameMenuActivity.class);
			intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent2);
			break;
		}

	}

}